package com.naihe.anderson.sms.widgets;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import com.naihe.anderson.sms.activity.FriendCheckActivity;

public class MyView extends View {

	private Handler handler;
	public MyView(Context context) {
		super(context);
	}

	public MyView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public MyView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}

	private String py[] = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J",
			"K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W",
			"X", "Y", "Z" };

	@Override
	public void draw(Canvas canvas) {
		super.draw(canvas);
	}

	public void setHandler(Handler handler){
		this.handler = handler;
	}
	
	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		int len = py.length;
		int i1 = getHeight();
		int i2 = getWidth();
		int i3 = i1 / len;
		int i4 = i3 * len;
        
		float f1 = (i1 - i4) / 2 + i3;
		float f2 = i2 / 2 - 5;
		Paint localPaint1 = new Paint();
		if(actionDown){
			localPaint1.setColor(Color.parseColor("#03b4c9"));
			canvas.drawCircle(f2 + 5, cy, 10.0f, localPaint1);
		}
        
		localPaint1.setColor(Color.WHITE);
		localPaint1.setTextSize(16);
		for (int i = 0; i < py.length; i++) {
			String str2 = py[i];
			float f16 = i3 * i + f1 - 2;
			canvas.drawText(str2, f2, f16, localPaint1);
		}
	}

	float cy;
	boolean actionDown = false;
	
	@Override
	public boolean dispatchTouchEvent(MotionEvent event) {
		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:
			actionDown = true;
			cy = event.getY();
			setBackgroundColor(Color.parseColor("#999999"));
			//invalidate(); 
			break;
		case MotionEvent.ACTION_MOVE:
			int i2 = py.length;
	        int i5 = getHeight();
	        int i6 = i5 / i2;
	        int i7 = i6 * i2;
	        float f2 = (i5 - i7) / 2;
	        cy = event.getY();
	        f2 = cy - f2;
	        float f3 = i6;
	        
	        Message message = new Message();
	        Bundle data = new Bundle();
	        data.putInt("localObject", (int)(f2 / f3));
	        message.setData(data);
	        message.what = FriendCheckActivity.SHOW_VIEW_PY;
	        handler.sendMessage(message);
			invalidate();
			break;
		case MotionEvent.ACTION_UP:
//			cy = event.getY();
			actionDown = false;
			setBackgroundColor(Color.parseColor("#dddddd"));
			handler.sendEmptyMessage(FriendCheckActivity.SHOW_VIEW_UP);
			invalidate();
			break;
		}
		return true;
	}


}
